#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        int m = nums.size();
        int sum = 0;
        for (auto& e : nums) sum += e;
        int n = (target + sum) / 2;

        if (n < 0 || (target + sum) % 2) return 0;

        vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
        dp[0][0] = 1;
        for (int i = 1;i <= m;i++)
        {
            for (int j = 0;j <= n;j++)
            {
                dp[i][j] = dp[i - 1][j];
                if (j >= nums[i - 1]) dp[i][j] += dp[i - 1][j - nums[i - 1]];
            }
        }
        return dp[m][n];
    }
};